package load.properties;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.util.Map;
import java.util.Properties;

public class Path {
	
	/*****************************************************************************/
	
	private static Map<String, String> MAP = null;
	private static String PATH_FILE = "../../file/properties/paths.properties";
	private Properties properties;

	/*****************************************************************************/
	/*************************Chaves Dos Paths**********************************/
	/*****************************************************************************/
	
	public static final String PATH_NCM_IMPORT = "pathNcmImport";
	public static final String PATH_NCM_EXPORT = "pathNcmExport";
	public static final String PATH_PISCOFIS_IMPORT = "pathPisCofinsImport";
	public static final String PATH_MEDIDA_IMPORT = "pathMedidaImport";
	
	/*****************************************************************************/
	/***********************Fim das Chaves dos Paths******************************/
	/*****************************************************************************/

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public Path getInstance() {
		try {
			InputStream stream = getClass().getResourceAsStream(PATH_FILE);
			getProperties().load(stream);
			MAP = (Map) getProperties();
			stream.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return this;
	}

	private static Map<String, String> getMap() {
		if (MAP == null)
			(new Path()).getInstance();
		return MAP;
	}
	public static String getValue(String key) {
		return getMap().get(key);
	}
	public void setProperties(String key, String value) {
        InputStream in;
		try {
			in = getInstance().getClass().getResource(PATH_FILE).openStream();
			getProperties().load(in);
	        File file = new java.io.File(getInstance().getClass().getResource(PATH_FILE).toURI());
	        FileOutputStream out = new FileOutputStream(file);
	        getProperties().setProperty(key, value);
	        getProperties().store(out, "alterado");
	        in.close();
	        out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (URISyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void setProperties(Properties properties) {
		this.properties = properties;
	}

	public Properties getProperties() {
		if (properties == null) {
			properties = new Properties();
		}
		return properties;
	}
}
